package com.solution.admin.codegen.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.portlet.ModelAndView;

import com.solution.common.codegen.CodeBuilder;
import com.solution.common.codegen.ColBean;
import com.solution.common.controller.BaseController;
import com.solution.common.utils.string.StringUtil;

@Controller
@RequestMapping("/")
public class CodegenController extends BaseController {

	
	/**
	 * 查询列表信息
	 * @param request
	 * @return
	 */
	@RequestMapping("/col_settings")
	public String listCol(HttpServletRequest request){
		CodeBuilder cb = new CodeBuilder();
		String tableName = request.getParameter("tableName");
		ModelAndView mav = new ModelAndView();
		List<ColBean> colList = cb.getTableColumns(null, tableName);
		request.setAttribute("colList", colList);
		request.setAttribute("tableName", tableName);
		
		// 表名列表
		List<String[]> tableList = cb.getAllTableList("");
		request.setAttribute("tableList", tableList);
		
		return "codegen/codegen";
	}
	
	
	/**
	 * 更新列表备注
	 * @param request
	 */
	@SuppressWarnings("unused")
	@RequestMapping("/update_col_settings")
	public String updateColComment(HttpServletRequest request){
		String tableName = request.getParameter("tableName");
		String[] conditionArr = request.getParameterValues("condition");
		String[] conditionTypeArr = request.getParameterValues("condition_type");
		String[] showListArr = request.getParameterValues("show_list");
		String[] showFormArr = request.getParameterValues("show_form");
		
		String[] insert_defaultArr = request.getParameterValues("insert_default");
		String[] controller_defaultArr = request.getParameterValues("controller_default");
		
		
		Map<String, Object> conditionMap = new HashMap<String, Object>();
		Map<String, Object> conditionTypeMap = new HashMap<String, Object>();
		Map<String, Object> showListMap = new HashMap<String, Object>();
		Map<String, Object> showFormMap = new HashMap<String, Object>();
		Map<String, Object> insertDefaultMap = new HashMap<String, Object>();
		Map<String, Object> controllerDefaultMap = new HashMap<String, Object>();
		
		
		for(String condition : conditionArr){
			conditionMap.put(condition, 1);
		}
		int i=0;
		for(String conditionType : conditionTypeArr){
			conditionTypeMap.put(String.valueOf(i), conditionType);
			i++;
		}
		for(String showList : showListArr){
			showListMap.put(showList, 1);
		}
		for(String showForm : showFormArr){
			showFormMap.put(showForm, 1);
		}
		i=0;
		for (String insertDefault : insert_defaultArr) {
			insertDefaultMap.put(String.valueOf(i), insertDefault);
			i++;
		}
		i=0;
		for (String controllerDefault : controller_defaultArr) {
			controllerDefaultMap.put(String.valueOf(i), controllerDefault);
			i++;
		}
		
		CodeBuilder cb = new CodeBuilder();
		List<ColBean> colList = cb.getTableColumns(null, tableName);
		for (ColBean cbl : colList) {
			StringBuffer tmpBuf = new StringBuffer();
			String tmpInsertDefault = (String)insertDefaultMap.get(String.valueOf(colList.indexOf(cbl)));
			String tmpControllerDefault = (String)controllerDefaultMap.get(String.valueOf(colList.indexOf(cbl)));
			tmpBuf.append(cbl.getColCom()).append("|");
			tmpBuf.append(conditionMap.containsKey(cbl.getPropertyName())?"Y":"N").append("#")
				  .append(conditionTypeMap.get(String.valueOf(colList.indexOf(cbl)))).append("#")
				  .append(showListMap.containsKey(cbl.getPropertyName())?"Y":"N").append("#")
				  .append(showFormMap.containsKey(cbl.getPropertyName())?"Y":"N").append("#")
				  .append(StringUtil.isEmpty(tmpInsertDefault)?"NG":tmpInsertDefault).append("#")
				  .append(StringUtil.isEmpty(tmpControllerDefault)?"NG":tmpControllerDefault);
			cbl.setColComName(tmpBuf.toString());
		}
		cb.updateTableComment(tableName, colList);
		return "redirect:/col_settings";
	}
}
